package resolve;

import cn.org.rapid_framework.generator.util.PropertyPlaceholderHelper;

import java.util.Iterator;
import java.util.Properties;

public class PropertiesHelper {

    boolean isSearchSystemProperty = false;
    Properties p;
    static PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}", ":", false);


    public PropertiesHelper(Properties p, boolean isSearchSystemProperty) {
        this.p = resolveProperties(p);
        this.isSearchSystemProperty = isSearchSystemProperty;
    }

    private static Properties resolveProperties(Properties props) {
        Properties result = new Properties();
        Iterator i$ = props.keySet().iterator();

        while(i$.hasNext()) {
            Object s = i$.next();
            String sourceKey = s.toString();
            String key = resolveProperty(sourceKey, props);
            String value = resolveProperty(props.getProperty(sourceKey), props);
            result.setProperty(key, value);
        }
        return result;
    }

    private static String resolveProperty(String v, Properties props) {
        PropertyPlaceholderHelper.PropertyPlaceholderConfigurerResolver propertyPlaceholderConfigurerResolver = new PropertyPlaceholderHelper.PropertyPlaceholderConfigurerResolver(props);
        return propertyPlaceholderHelper.replacePlaceholders(v, propertyPlaceholderConfigurerResolver);
    }
}
